<template>
  <div class="login-container">
    <div class="login-card">
      <!-- 左侧品牌区域 -->
      <div class="brand-section">
        <div class="brand-content">
          <div class="brand-header">
            <i class="fas fa-graduation-cap"></i>
            <h1>智慧考试系统</h1>
          </div>
          
          <div class="features">
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fas fa-shield-alt"></i>
              </div>
              <div class="feature-text">
                <h3>安全可靠</h3>
                <p>采用JWT加密技术，确保考试数据安全与用户身份验证</p>
              </div>
            </div>
            
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fas fa-users"></i>
              </div>
              <div class="feature-text">
                <h3>多角色管理</h3>
                <p>支持管理员与学生双角色系统，满足不同用户需求</p>
              </div>
            </div>
            
            <div class="feature-item">
              <div class="feature-icon">
                <i class="fas fa-mobile-alt"></i>
              </div>
              <div class="feature-text">
                <h3>响应式设计</h3>
                <p>完美适配PC与移动端，随时随地进行考试与管理</p>
              </div>
            </div>
          </div>
        </div>
        
        <div class="brand-image">
          <img src="https://design.gemcoder.com/staticResource/echoAiSystemImages/4a59c1a4e78f15392ae23b7f5447f813.png" alt="教育科技插图" />
        </div>
      </div>
      
      <!-- 右侧表单区域 -->
      <div class="form-section">
        <!-- 移除角色选择，统一使用账号密码登录 -->
        
        <!-- 登录表单 -->
        <div class="form-content">
          <div class="form-header">
            <h2>欢迎回来</h2>
            <p>请输入您的账号信息登录系统</p>
          </div>
          
          <el-form 
            ref="loginFormRef" 
            :model="loginForm" 
            :rules="loginRules" 
            @submit.prevent="handleLogin"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                placeholder="请输入用户名"
                size="large"
                :prefix-icon="User"
              />
            </el-form-item>
            
            <el-form-item prop="password">
              <el-input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                size="large"
                :prefix-icon="Lock"
                show-password
                @keyup.enter="handleLogin"
              />
            </el-form-item>
            
            <el-form-item>
              <div class="form-options">
                <el-checkbox v-model="loginForm.remember">记住我</el-checkbox>
              </div>
            </el-form-item>
            
            <el-form-item>
              <el-button 
                type="primary" 
                size="large" 
                :loading="loading" 
                @click="handleLogin"
                class="login-btn"
              >
                {{ loading ? '登录中...' : '登录' }}
              </el-button>
            </el-form-item>
          </el-form>
          
          <div class="form-footer">
            <p>
              还没有账号？ 
              <router-link to="/register" class="register-link" >
                立即注册
              </router-link>
            </p>
          </div>
          
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, watch } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import { useUserStore } from '@/stores'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref()
const loading = ref(false)
const loginForm = reactive({
  username: '',
  password: '',
  remember: false
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

// 移除角色切换监听

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const result = await userStore.loginAction({
      ...loginForm
    })
    
    if (result.success) {
      ElMessage.success('登录成功')
      
      // 确保用户信息已设置
      console.log('登录成功，用户信息:', userStore.userInfo)
      console.log('用户角色:', userStore.userInfo?.role)
      
      
      // 等待一小段时间确保状态更新
      await new Promise(resolve => setTimeout(resolve, 100))
      
      // 根据后端返回的用户角色跳转到对应页面
      const userRole = userStore.userInfo?.role


      if (userRole === 'super_admin') {
        console.log('跳转到超级管理员页面')
        router.push('/super-admin')
      } else if (userRole === 'teacher') {
        console.log('跳转到教师页面')
        router.push('/teacher')

      } else if (userRole === 'student') {
        console.log('跳转到学生页面')
        router.push('/student/dashboard')
      } else {
        console.log('未知角色，跳转到默认页面')
        router.push('/')
      }
    } else {
      ElMessage.error(result.message)
    }
  } catch (error) {
    console.error('登录失败:', error)
  } finally {
    loading.value = false
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
}

.login-card {
  width: 100%;
  max-width: 1000px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  display: flex;
  min-height: 600px;
  
  @media (max-width: 768px) {
    flex-direction: column;
    max-width: 400px;
  }
}

.brand-section {
  flex: 1;
  background: linear-gradient(135deg, #165DFF 0%, #0A3CA0 100%);
  color: white;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  
  @media (max-width: 768px) {
    padding: 30px 20px;
  }
}

.brand-header {
  display: flex;
  align-items: center;
  margin-bottom: 40px;
  
  i {
    font-size: 32px;
    margin-right: 12px;
  }
  
  h1 {
    font-size: 28px;
    font-weight: 700;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    
    @media (max-width: 768px) {
      font-size: 24px;
    }
  }
}

.features {
  .feature-item {
    display: flex;
    align-items: flex-start;
    margin-bottom: 32px;
    
    .feature-icon {
      background: rgba(255, 255, 255, 0.2);
      padding: 12px;
      border-radius: 12px;
      margin-right: 16px;
      
      i {
        font-size: 20px;
      }
    }
    
    .feature-text {
      h3 {
        font-size: 18px;
        font-weight: 600;
        margin-bottom: 8px;
      }
      
      p {
        color: rgba(255, 255, 255, 0.8);
        line-height: 1.5;
      }
    }
  }
}

.brand-image {
  img {
    width: 100%;
    height: auto;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
  }
}

.form-section {
  flex: 1;
  padding: 40px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  
  @media (max-width: 768px) {
    padding: 30px 20px;
  }
}

.role-tabs {
  display: flex;
  border-bottom: 2px solid #f0f0f0;
  margin-bottom: 32px;
  
  .role-tab {
    flex: 1;
    padding: 16px 0;
    border: none;
    background: none;
    font-size: 16px;
    font-weight: 500;
    color: #999;
    cursor: pointer;
    transition: all 0.3s ease;
    border-bottom: 2px solid transparent;
    
    i {
      margin-right: 8px;
    }
    
    &.active {
      color: var(--primary-color);
      border-bottom-color: var(--primary-color);
    }
    
    &:hover {
      color: var(--primary-color);
    }
  }
}

.form-header {
  margin-bottom: 32px;
  
  h2 {
    font-size: 32px;
    font-weight: 700;
    color: #333;
    margin-bottom: 8px;
    
    @media (max-width: 768px) {
      font-size: 28px;
    }
  }
  
  p {
    color: #666;
    font-size: 16px;
  }
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
}

.login-btn {
  width: 100%;
  height: 48px;
  font-size: 16px;
  font-weight: 600;
}

.form-footer {
  text-align: center;
  margin-top: 24px;
  
  p {
    color: #666;
  }
}

.register-link {
  color: #409EFF;
  text-decoration: none;
  font-weight: 600;
  transition: color 0.3s ease;
}

.register-link:hover {
  color: #764ba2;
}

:deep(.el-form-item) {
  margin-bottom: 24px;
}

:deep(.el-input__wrapper) {
  padding: 12px 16px;
  border-radius: 8px;
}
</style>